package org.ezplatform.office.forum.web.rest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.ezplatform.cloud.api.system.permission.PermissionApiService;
import org.ezplatform.cloud.common.controller.BaseController;
import org.ezplatform.core.common.WebUser;
import org.ezplatform.core.web.util.WebUtils;
import org.ezplatform.core.web.view.OperationResult;
import org.ezplatform.office.forum.service.ForumBoardService;
import org.ezplatform.office.forum.service.ForumPostService;
import org.ezplatform.util.GlobalConstant;
import org.ezplatform.util.JwtUtils;
import org.ezplatform.util.StringUtils;
import org.ezplatform.util.UuidUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 论坛接口
 * 2019年11月2日17:40:59
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/api/oaForum")
public class ForumApiController extends BaseController {

	private static final Logger LOG = LoggerFactory.getLogger(ForumApiController.class);

	@Autowired
	private ForumBoardService forumBoardService;

	@Autowired
	private ForumPostService forumPostService;

	@Autowired
	private PermissionApiService permissionApiService;

	private final String cardNumber = "3";

	/********************************** 以下是门户调用 ********************************/
	@ApiOperation(value = "获取论坛版块下拉树", notes = "获取论坛版块下拉树")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "corpId", value = "用户ID", required = true, paramType = "path", dataType = "String") })
	@RequestMapping(value = "/getBoardTree", method = RequestMethod.GET, produces = {
			"application/json; charset=UTF-8" })
	public OperationResult getBoardTree(HttpServletRequest request) {
		WebUser user = WebUtils.getCurrentUser();
		String corpId = request.getParameter("corpId");
		List<Map> result = forumBoardService.getTypeBoardTree("", corpId, "1", user);
		List<Map<String,Object>> tree = new ArrayList();
		if(CollectionUtils.isNotEmpty(result)) {
			for(Map m : result) {
				Map<String,Object> map = new HashMap();
				map.put("id", StringUtils.null2String(m.get("id")));
				map.put("pId", StringUtils.null2String(m.get("pId")));
				map.put("name", StringUtils.null2String(m.get("name")));
				if(StringUtils.equals("1", StringUtils.null2String(m.get("pId")))){
					map.put("nocheck", "true");
				}else {
					map.put("nocheck", "false");
				}
				tree.add(map);
			}
		}
		return OperationResult.buildSuccessResult("查询版块树成功",tree);
	}

	@ApiOperation(value = "获取论坛帖子列表", notes = "根据数量范围+版块来获取帖子列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "corpId", value = "单位ID", required = true, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "showNumber", value = "列表显示条数", required = true, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "boardIds", value = "版块Ids", required = true, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "userName", value = "用户名", required = false, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "token", value = "token", required = true, paramType = "path", dataType = "String") })
	@RequestMapping(value = "/getBoardPostList", method = RequestMethod.GET, produces = {
			"application/json; charset=UTF-8" })
	public OperationResult getBoardPostList(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		String userName = request.getParameter("userName");
		WebUser user = WebUtils.getCurrentUser();
		String corpId = request.getParameter("corpId");
		String showNumber = request.getParameter("showNumber");
		String boards = request.getParameter("boardIds");
		String var2 = request.getHeader("Authorization");
		String token = StringUtils.isNotBlank(var2) && var2.startsWith("Bearer ") ? var2.substring(7, var2.length())
				: null;
		List<String> boardList = new ArrayList<String>();
		if (StringUtils.isNotBlank(boards)) {
			boardList = Arrays.asList(boards.split(","));
		}
		List<Map> resutl = forumPostService.getBoardPostList(token, boardList, corpId, showNumber);
		if (CollectionUtils.isNotEmpty(resutl)) {
			resutl.get(0).put("ifReadAll", forumPostService.ifReadAllWithDingding(user.getUserId()));
		}
		return OperationResult.buildSuccessResult(resutl);
	}

	@ApiOperation(value = "获取论坛分类帖子列表", notes = "论坛分类帖子列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "corpId", value = "单位ID", required = true, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "showNumber", value = "列表显示条数", required = true, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "boardIds", value = "版块Ids", required = true, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "userName", value = "用户名", required = false, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "token", value = "token", required = true, paramType = "path", dataType = "String") })
	@RequestMapping(value = "/getTypePostList", method = RequestMethod.GET, produces = {
			"application/json; charset=UTF-8" })
	public OperationResult getTypePostList(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		WebUser user = WebUtils.getCurrentUser();
		String userName = request.getParameter("userName");
		String corpId = request.getParameter("corpId");
		String showNumber = request.getParameter("showNumber");
		List<Map> resutl = new ArrayList<>();
		// 分类选择1.最新发帖,2.最新回帖,3.热门帖子,4.精华帖子
		String selectType = request.getParameter("selectType");
		if (selectType != null && StringUtils.isNotBlank(selectType)) {
			selectType = selectType.substring(1, selectType.length() - 1);
			String[] type = selectType.split(",");
			String var2 = request.getHeader("Authorization");
			String token = StringUtils.isNotBlank(var2) && var2.startsWith("Bearer ") ? var2.substring(7, var2.length())
					: null;
			Map map = new HashMap<>();
			List<Map> list = new ArrayList<>();
			for (String string : type) {
				string = string.trim();
				list = new ArrayList<>();
				map = new HashMap<>();
				list = forumPostService.getTypePostList(token, string, corpId, showNumber, userId, userName,
						user.getOrgId());
				map.put("moduleData", list);
				if ("1".equals(string)) {
					map.put("type", "最新发帖");
					map.put("id", string);
					map.put("i18nCode", "oa.forum.newposts");
				} else if ("2".equals(string)) {
					map.put("type", "最新评论");
					map.put("id", string);
					map.put("i18nCode", "oa.forum.comments");
				} else if ("3".equals(string)) {
					map.put("type", "热门帖子");
					map.put("id", string);
					map.put("i18nCode", "oa.forum.hottips");
				} else if ("4".equals(string)) {
					map.put("type", "精华帖子");
					map.put("id", string);
					map.put("i18nCode", "oa.forum.essposts");
				}
				resutl.add(map);
			}
		}
		if (CollectionUtils.isNotEmpty(resutl)) {
			resutl.get(0).put("ifReadAll", forumPostService.ifReadAllWithDingding(user.getUserId()));
		}
		return OperationResult.buildSuccessResult(resutl);
	}

	@ApiOperation(value = "获取论坛版块推荐列表", notes = "论坛版块推荐列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "corpId", value = "单位ID", required = true, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "showNumber", value = "列表显示条数", required = true, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "userName", value = "用户名", required = false, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "token", value = "token", required = true, paramType = "path", dataType = "String") })
	@RequestMapping(value = "/getRecommendBoardList", method = RequestMethod.GET, produces = {
			"application/json; charset=UTF-8" })
	public OperationResult getRecommendBoardList(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		String userName = request.getParameter("userName");
		String corpId = request.getParameter("corpId");
		String showNumber = request.getParameter("showNumber");
		List<Map> resutl = new ArrayList<>();
		String var2 = request.getHeader("Authorization");
		String token = StringUtils.isNotBlank(var2) && var2.startsWith("Bearer ") ? var2.substring(7, var2.length())
				: null;
		Map map = new HashMap<>();
		List<Map> list = new ArrayList<>();
		WebUser user = WebUtils.getCurrentUser();
		list = forumPostService.getRecommendBoardList(token, corpId, showNumber,userId);
		map.put("moduleData", list);
		map.put("type", "版块推荐");
		map.put("id", "1");
		resutl.add(map);
		return OperationResult.buildSuccessResult(resutl);
	}

	@ApiOperation(value = "获取论坛卡片数据", notes = "获取论坛卡片数据")
	@ApiImplicitParams({ 
			@ApiImplicitParam(name = "corpId", value = "单位ID", required = true, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "showNumber", value = "列表显示条数", required = true, paramType = "path", dataType = "String"),
			@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "path", dataType = "String"), })
	@RequestMapping(value = "/getPostCardList", method = RequestMethod.GET, produces = {
			"application/json; charset=UTF-8" })
	public OperationResult getPostCardList(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		String corpId = request.getParameter("corpId");
		String showNumber = request.getParameter("showNumber");
		Map<String, String> textParams = new HashMap<String, String>();
		String jwtToken = JwtUtils.createJwtToken(UuidUtils.UID(), GlobalConstant.X_APP_JOB,
				GlobalConstant.JWT_TOKEN_SERVER, JwtUtils.getTimeout(), textParams);
		String var2 = request.getHeader("Authorization");
		List<Map> list = new ArrayList<>();
		if (showNumber == null || StringUtils.isBlank(showNumber)) {
			showNumber = cardNumber;
		}
		list = forumPostService.getPostCardList(corpId, showNumber, userId, jwtToken);
		return OperationResult.buildSuccessResult(list);
	}
	
	@ApiOperation(value = "获取论坛卡片数据", notes = "获取论坛卡片数据")
	@ApiImplicitParams({ 
			@ApiImplicitParam(name = "userId", value = "单位ID", required = true, paramType = "path", dataType = "String"),})
	@RequestMapping(value = "/getAplicationBoardId", method = RequestMethod.GET, produces = {
			"application/json; charset=UTF-8" })
	public OperationResult getAplicationBoardId(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		List<Map> list = forumBoardService.getSearchPostId(userId);
		List<String> returnList = new ArrayList();
		if(CollectionUtils.isNotEmpty(list)) {
			for(Map m : list) {
				returnList.add(StringUtils.null2String(m.get("pid")));
			}
		}
		return OperationResult.buildSuccessResult(returnList);
	}
	
	@ApiOperation(value = "回复和评论数量", notes = "回复和评论数量")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "path", dataType = "String") })
	@RequestMapping(value = "/getCommentNum", method = RequestMethod.GET, produces = {
			"application/json; charset=UTF-8" })
	public OperationResult getCommentNum(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		List<Map> allCommentsList = forumPostService.findIfRead(userId, "2");
		List<Map> allReplyList = forumPostService.findIfRead(userId, "3");
		Integer allCommentsNum = null;
		Integer allReplyNum = null;
		if(CollectionUtils.isNotEmpty(allCommentsList)) {
			allCommentsNum = allCommentsList.size();
		}else {
			allCommentsNum = 0;
		}
		System.out.println("allCommentsNum:"+allCommentsNum);
		if(CollectionUtils.isNotEmpty(allReplyList)) {
			allReplyNum = allReplyList.size();
		}else {
			allReplyNum = 0;
		}
		
		Integer num = allCommentsNum + allReplyNum;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("count", num);
		return OperationResult.buildSuccessResult("查询回复和评论数量总和成功",map);
	}
}
